package kr.saeildamil.androidstudty.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudty.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MultiAdapter extends BaseAdapter {
LayoutInflater mInflater;
ArrayList<ListItem> arSrc;
	



public MultiAdapter(Context context, ArrayList<ListItem> arItem) {

	mInflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	arSrc = arItem;
}

@Override
	public int getCount() {
		
		return arSrc.size();
	}

	@Override
	public ListItem getItem(int position) {
		
		return arSrc.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		
		return arSrc.get(position).Type;
	}
	public int getViewTypeCount(){
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null){
			int res = 0;
			switch (arSrc.get(position).Type){
			case 0:
				res = R.layout.ch12_textedit;
				break;
			case 1:
				res =R.layout.ch12_btnicon;
				break;
			}
			convertView = mInflater.inflate(res, parent, false);
		}
		
		switch (arSrc.get(position).Type){
		case 0:
			TextView txt = (TextView)convertView.findViewById(R.id.ch12_textedit_text);
			txt.setText(arSrc.get(position).Text);
			EditText edit=(EditText)convertView.findViewById(R.id.ch12_textedit_edit);
			edit.setText(arSrc.get(position).EditText);
			break;
		case 1:
			Button btn = (Button)convertView.findViewById(R.id.ch12_icontext_btn);
			btn.setText(arSrc.get(position).BtnText);
			ImageView img =(ImageView)convertView.findViewById(R.id.ch12_icontext_img);
			img.setImageResource(arSrc.get(position).IconRes);
		}
		return convertView;
		
	}

	
}
